package com.myshoutui;

import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Hashtable;

import com.myshout.Client;
import com.myshout.Person;
import com.myshout.android.commmon.Utils;
import com.myshout.rest.RestClient;
import com.myshout.rest.RestClientException;
import com.myshout.rest.RestResult;
import com.myshoutui.manager.MyShoutConsumerAsynTaskInterface;
import com.myshoutui.manager.MyShoutConsumerAsynTaskManager;
import com.myshoutui.manager.MyShoutDataManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyShoutConsumerLoginActivity extends Activity implements
		MyShoutConsumerAsynTaskInterface, OnClickListener {

	Button mLoginbtn;
	Button mFacebookbtn;
	Button mTwitterbtn;
	EditText mUserEmail;
	EditText mPassword;
	Person user;
	private ProgressDialog progressDialog;
	MyShoutConsumerAsynTaskInterface mAsynListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		this.mAsynListener = this;
		intializeViewComponents();
	}

	private void intializeViewComponents() {
		mLoginbtn = (Button) findViewById(R.id.loginlogbtn);
		mFacebookbtn = (Button) findViewById(R.id.loginfacebookbtn);
		mTwitterbtn = (Button) findViewById(R.id.logintwitterbtn);
		mUserEmail = (EditText) findViewById(R.id.useremail);
		mUserEmail.setText("nduvvuru09@gmail.com");
		mPassword = (EditText) findViewById(R.id.userpassword);
		mPassword.setText("bliss2012");
		mLoginbtn.setTypeface(Typeface.createFromAsset(getAssets(),
				"fonts/fontscore.com_h_helveticacondensed-bold.ttf"));
		mFacebookbtn.setTypeface(Typeface.createFromAsset(getAssets(),
				"fonts/fontscore.com_h_helveticacondensed-bold.ttf"));
		mTwitterbtn.setTypeface(Typeface.createFromAsset(getAssets(),
				"fonts/fontscore.com_h_helveticacondensed-bold.ttf"));
		mLoginbtn.setOnClickListener(this);
		mFacebookbtn.setOnClickListener(this);
		mTwitterbtn.setOnClickListener(this);

	}

	public Boolean doLogin(final String username, final String password) {

		Hashtable<String, String> nvPairs = new Hashtable<String, String>();
		nvPairs.put("username", username);
		nvPairs.put("password", password);
		RestResult result;
		try {
			result = RestClient.post(Client.personLoginResource, nvPairs);
			user = Client.getGson().fromJson(result.getDataAsString(),
					Person.class);
			MyShoutDataManager app = (MyShoutDataManager) getApplicationContext();
			user.setUsername(username);
			user.setPassword(password);
			app.setUser(user);
			RestClient.setCredentials(username, password);
			return true;
		} catch (RestClientException e) {
			e.printStackTrace();
			Log.e("", "Login wrong");
			return false;
		} finally {
			Log.e("", "Login suceesfully finally called");
		}

	}

	@Override
	public void startTask() {
		// TODO Auto-generated method stub
		progressDialog = ProgressDialog.show(this, "",
				getString(R.string.please_wait_), true);
	}

	@Override
	public void endTask(Object task) {
		// TODO Auto-generated method stub
		try{
		if(progressDialog.isShowing()){
		progressDialog.dismiss();
		}
		if ((Boolean) task) {
			startActivity(new Intent(getApplicationContext(),
					MyShoutDashboardActivity.class));
			finish();
		} else {
			Toast.makeText(getApplicationContext(),
					"Invalid User Name and Password", Toast.LENGTH_LONG).show();
		}
		}catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object executeTask(Object... task) {
		return doLogin(((Person) task[0]).getUsername(),
				((Person) task[0]).getPassword());

	}

	@SuppressWarnings("unchecked")
	@Override
	public void onClick(View v) {

		if (v.getId() == R.id.loginlogbtn) {
			
			if (mUserEmail.getText().toString().trim().length() == 0
					|| mPassword.getText().toString().trim().length() == 0) {
				Toast.makeText(getApplicationContext(),
						"Please Enter Email Address and Password",
						Toast.LENGTH_LONG).show();
			}else{
				Person data = new Person();
				data.setUsername(mUserEmail.getText().toString().trim());
				data.setPassword(mPassword.getText().toString().trim());
				new MyShoutConsumerAsynTaskManager(mAsynListener).execute(data);
			}

		} else if (v.getId() == R.id.loginfacebookbtn) {

		} else if (v.getId() == R.id.logintwitterbtn) {

		} else {

		}
	}

}
